/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-11-05
 * Time: 15:05
 */

//循环队列
public class MyCircularQueue {
    //定义一个数组
    public int[] elem;
    //头下标
    public int front;
    //尾下标
    public int rear;

    public MyCircularQueue(int k){
        this.elem = new int[k+1];
    }

    //入队列
    public void push(int val){
        //判断队列满不满
        if ((rear+1)%elem.length == front){
            System.out.println("队列已满!");
            return;
        }
        elem[rear] = val;
        rear = (rear+1)%elem.length;
    }

    //出队列 并且得到队首元素
    public int pop(){
        //判断是否为空
        if (rear == front){
            System.out.println("队列为空!");
            return -1;
        }
        int ret = elem[front];
        front = (front+1) % elem.length;
        return ret;
    }

    //得到队尾元素
    public void tail(){
        if (rear == front){
            System.out.println("队列为空!");
        }
        int index;
        if (rear == 0){
            index = elem.length-1;
        }else {
            index = rear-1;
        }
        System.out.println(elem[index]);
    }
}
